﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Mozlite.Web.UI.Models;
using Mozlite.Common;
using System.Web.Security;
using Mozlite.Common.Configuration;

namespace Mozlite.Web.UI.Controllers
{
    public class AccountController : CommonBaseController
    {
        public ActionResult Logon()
        {
            return View(new LoginModel());
        }

        [HttpPost, HandleError]
        public ActionResult Logon(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AutoLoginCookie cookie = new AutoLoginCookie();
                if (!cookie.HasValue || (cookie.HasValue && cookie.GetValue() != model.AutoLogin))
                {
                    cookie.Write(model.AutoLogin);
                }
                LoginUserStatus status = Users.ValidUser(ValidMode.UserName, model.UserName, model.Password);
                if (status == LoginUserStatus.Success || (!Settings.EnableBannedUsersToLogin && status == LoginUserStatus.AccountBanned))
                {
                    if (!Settings.AllowLogin && !Current.User.IsAdministrator)
                    {
                        SetResourceStatus(LoginUserStatus.UserLoginDisabled);
                        return View(model);
                    }
                    Users.SignIn(model.AutoLogin);
                    Current.User.LastLoginIP = Request.UserHostAddress;
                    Current.User.SetLogin();
                    SetResourceStatus(status);
                    return View();
                }
                SetResourceStatus(status);
            }
            return View(model);
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost, HandleError]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User();
                user.UserName = model.UserName;
                user.Password = model.Password;
                user.Email = model.Email;
                user.PasswordQuestion = model.PasswordQuestion;
                user.PasswordAnswer = model.PasswordAnswer;
                CreateUserStatus status = Users.CreateUser(user, model.PublicKey);
                if (status == CreateUserStatus.Created)
                {
                    AppContext current = AppContext.Current;
                    current.User = user;
                    Users.SignIn(false);
                    if (current.IsWebRequest)
                    {
                        Users.RemoveAnonymousTracking(current.Context, SiteSettings.Default.AnonymousCookieName);
                    }
                }
                SetResourceStatus(status, status!= CreateUserStatus.Created);
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            Self.GetUserCookie().DeleteCookie();
            FormsAuthentication.SignOut();
            return Redirect(ReturnUrl ?? CommonUrls.DefaultUrl);
        }
    }
}
